﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace SSDUTElectricityChargesNotification
{
    class INIOperationClass
    {
        [DllImport("kernel32")]//返回0表示失败，非0为成功
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);


        public static bool WriteConfig(string section,string key,string value,string path)
        {
            return WritePrivateProfileString(section, key, value, path) != 0;
        }

        public static string ReadConfig(string Section, string Key, string NoText, string iniFilePath)//读取INI文件
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                if (GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath) > 0)
                {
                    return temp.ToString();
                }
            }
            return NoText == null ? String.Empty : NoText;
        }
    }
}
